package exam.exam01;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

/**
 * 分析以下需求，并用代码实现
 * 1.利用键盘录入，输入一个字符串
 * 2.统计该字符串中各个字符的数量(提示:字符不用排序)
 * 3.如：用户输入字符串
 * "If~you-want~to~change-your_fate_I_think~you~must~come-to-the-dark-horse-to-learn-java"
 * 4.程序输出结果为:
 * -(9)I(2)_(3)a(7)c(2)d(1)e(6)f(2)g(1)h(4)i(1)j(1)k(2)l(1)m(2)n(4)o(8)r(4)s(2)t(8)u(4)v(1)w(1)y(3)~(6)
 */
public class Test02 {
    public static void main(String[] args) {
        //排序
        TreeMap<Character, Integer> hm = new TreeMap<>((e1, e2) -> e1.compareTo(e2));
        System.out.println("输入一个字符串");
        for (char c : new Scanner(System.in).nextLine().toCharArray()) {
            int temp = hm.containsKey(c) ? hm.get(c) + 1 : 1;
            hm.put(c, temp);
        }
        for (Map.Entry<Character, Integer> entry : hm.entrySet()) {
            System.out.print(entry.getKey() + "(" + entry.getValue() + ")");
        }
    }
}
